package main

import (
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
)

type Method int

const (
	POST Method = iota
	GET
	OPTIONS
	HEAD
	PUT
	PATCH
	DELETE
)

/*type Headers struct {
	Accept string
	Accept_Charset
	Accept_Encoding
	Accept_Language
	Authorization
	Cache_Control
	Connection
	Content_Type
	Cookie
	Date
	Expect
	From
	Host
	If_Match
	If_Modified_Since
	If_None_Match
	If_Range
	If_Unmodified_Since
	Max_Forwards
	Pragma
	Proxy_Authorization
	Range
	Referer
	TE
	Trailer
	Transfer_Encoding
	Upgrade
	User_Agent
	Via
	Warning
}*/

func main() {
	var path string = "http://127.0.0.1:8080/echo"
	u, _ := url.ParseRequestURI(path)
	v := u.Query()            //取出url中的参数
	v.Add("name", "kangkang") //添加参数
	// u.RawQuery = v.Encode() //将参数编码到url

	log.Printf("Response Url : %s", u.String())
	log.Printf("Response Host: %s", u.Host)
	log.Printf("    Param is : %s", u.RawQuery)

	// methodGet(u.String())

	// methodHead(u.String())

	//methodPostForm(u.String(), &v)
}

func methodGet(url string) {
	resp, err := http.Get(url)
	if err != nil {
		log.Printf("http get error ", err)
	}
	defer resp.Body.Close()
	echoResponseInfo(resp)
}
func methodPost() {
	/*
	   application/x-www-form-urlencoded --在发送前编码所有字符（默认）
	   multipart/form-data 	--不对字符编码。在使用包含文件上传控件的表单时，必须使用该值。
	   text/plain --	空格转换为 "+" 加号，但不对特殊字符编码。
	*/
	resp, err := http.Post("http://127.0.0.1:8080/upload", "image/jpeg", &buf)
	http.Post(url, contentType, body)

}

func methodPostForm(url string, data *url.Values) {
	resp, err := http.PostForm(url, *data)
	if err != nil {
		log.Printf("http get error ", err)
	}
	defer resp.Body.Close()
	echoResponseInfo(resp)
}

func methodHead(url string) {
	resp, err := http.Head(url)
	if err != nil {
		log.Printf("http head error ", err)
	}
	defer resp.Body.Close()
	echoResponseInfo(resp)
}

func echoResponseInfo(resp *http.Response) {
	body, _ := ioutil.ReadAll(resp.Body)

	log.Println("----------Header----------")
	for k, v := range resp.Header {
		log.Printf("%s=%s", k, v[0])
	}

	log.Printf("  StatusCode : %d", resp.StatusCode)
	if resp.StatusCode == 200 {

		log.Printf("        Body : %s", string(body))
	} else {
		log.Printf("        error")
	}
}
